<?php

namespace App\Http\Controllers\Admin\Crm;

use App\Models\Crm\ArticleCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Crm\Article;

class ArticleController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_crm' => 'am-in',
            '_article' => 'am-active',
            'categories' => ArticleCategory::all_categories(),
            'cates' => ArticleCategory::all_cates(), //用于select表单
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $articles = Article::all_articles($request);
        return view('admin.crm.article.index', compact('articles'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Article $article)
    {
        return view('admin.crm.article.create', compact('article'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        Article::create($request->all());
        return redirect(route('crm.article.index'))->with('success', '添加文章成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Article $article)
    {
        return view('admin.crm.article.edit', compact('article'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $article = Article::find($id);
        $article->update($request->all());
        return redirect(route('crm.article.index'))->with('success', '更新文章成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Article::destroy($id);
    }

    /**
     * 改变属性
     */
    public function change_attr(Request $request)
    {
        $article = Article::find($request->id);
        $attr = $request->attr;
        $article->$attr = !$article->$attr;
        $article->save();
    }

    /**
     * 多选删除
     */
    public function delete_all(Request $request)
    {
        Article::destroy($request->check_id);
    }


}
